﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Dispatcher;
using CHPM.IC.ServiceLocator;

namespace CHPM.SL.TheService.Code
{
    public class ServiceLocatorInstanceProvider : IInstanceProvider
    {

        private readonly IServiceLocator _serviceLocator;
        private readonly Type _serviceType;

        public ServiceLocatorInstanceProvider(Type serviceType, IServiceLocator serviceLocator)
        {            
            this._serviceType = serviceType;
            this._serviceLocator = serviceLocator;
        }

        public object GetInstance(System.ServiceModel.InstanceContext instanceContext, System.ServiceModel.Channels.Message message)
        {
            return this._serviceLocator.GetService(this._serviceType);
        }

        public object GetInstance(System.ServiceModel.InstanceContext instanceContext)
        {
            return this._serviceLocator.GetService(this._serviceType);
        }

        public void ReleaseInstance(System.ServiceModel.InstanceContext instanceContext, object instance)
        {
            this._serviceLocator.Release(instance);
        }
    }
}
